<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>layui</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="../layui/dist/css/layui.css" media="all">
</head>
<body>

<table class="layui-table" lay-filter="demo" id="listData">
</table>

<script type="text/html" id="barDemo">
    <a class="layui-btn layui-btn-primary layui-btn-xs" lay-event="detail">查看</a>
    <a class="layui-btn layui-btn-xs" lay-event="edit">编辑</a>
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
</script>


<script src="../layui/dist/layui.js"></script>
<!-- 注意：如果你直接复制所有代码到本地，上述js路径需要改成你本地的 -->
<script>
    layui.use('table', function(){
        var table = layui.table;

        table.render({
            elem: '#listData'
            ,height: '510'
            ,url: '/layui/user.json'
            ,page: true
            //,size: 'sm'
            ,cols: [[ //表头
                {field: 'id', title: 'ID',sort: true, fixed: 'left'}
                ,{fixed: 'right', title: '操作', align: 'center', toolbar: '#barDemo'}
            ]]
            ,done: function(res, curr, count){
                if (res.code == 1) {
                    layer.alert(res.msg);
                }
                console.log(res);
                console.log(curr);
                console.log(count);
            }
        });

        //监听工具条
        table.on('tool(demo)', function(obj){
            var data = obj.data;
            console.log(data);
            if(obj.event === 'detail'){
                layer.msg('ID：'+ data.id + ' 的查看操作');
            } else if(obj.event === 'del'){
                layer.confirm('真的删除行么', function(index){
                    obj.del();
                    layer.close(index);
                });
            } else if(obj.event === 'edit'){
                layer.alert('编辑行：<br>'+ JSON.stringify(data))
            }
        });

    });
</script>

</body>
</html>